﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Project.DTO;
using Project.BUS;
using System.Globalization;

namespace Project
{
    public partial class Upload : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Cookies["user"] != null) { 
            //lbus.Text = Server.HtmlEncode(Request.Cookies["user"].Value);
            }
            else
            {
                Response.Status = "301 Moved Permanently";
                Response.AddHeader("Location", "http://localhost:8788/home.aspx");
            }
            gvListUser.DataSource = BUS.userBUS.getAllUser();
            gvListUser.DataBind();
        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            string  titleVideo,fileSize, urlVideo, uploadDate, description, fileName;

            videoDTO vdt = new videoDTO();
            userDTO ust = new userDTO();
            ust = BUS.userBUS.getUserByUserName(Server.HtmlEncode(Request.Cookies["user"].Value));
            int uploaderID = ust.idUser;//Uploader           
            string path = Server.MapPath(".")+"\\video\\";

            try {

                HttpPostedFile file = fuFile.PostedFile;
                byte[] buffer = new byte[file.ContentLength];
                int bytesReaded = file.InputStream.Read(buffer, 0,fuFile.PostedFile.ContentLength);
                if (bytesReaded > 0 && bytesReaded < 32000000)
                {

                    titleVideo = tbTitleVideo.Text.Trim();//Title video
                    fileSize = (1.0 * fuFile.PostedFile.ContentLength / 1024000).ToString("F2", CultureInfo.InvariantCulture);//File size                    
                    uploadDate = DateTime.Now.Day+"-"+DateTime.Now.Month+"-"+DateTime.Now.Year;//Upload date

                    fileName = fuFile.PostedFile.FileName;
                    fileName = System.IO.Path.GetFileName(fileName);
                    //fuFile.PostedFile.SaveAs(@"video/" + fileName);
                    fuFile.SaveAs(path + fileName);

                    urlVideo = "\\video\\" + fileName;//URL video
                    description = "None";//Description

                    //insert new video to db
                    vdt.titleVideo = titleVideo;
                    vdt.uploader = uploaderID;
                    vdt.fileSize = fileSize;
                    vdt.uploadDate = uploadDate;
                    vdt.urlVideo = urlVideo;
                    vdt.description = description;

                    try {
                        Boolean rs = videoBUS.insertNewAVideo(vdt);
                        if (rs)
                        {
                            lbus.Text = "Upload successed.";
                        }
                        else {
                            lbus.Text = "Upload failed.";
                            return;
                        }
                    }
                    catch { }

                }
                else {
                    lbus.Text = "Filesize is large. Limited 30Mb";
                }               
                
                
            }
            catch (Exception ex) {
                Response.Write("Exception Occured:   " + ex);
            }
        }
    }
}